package com.shanglida.main.set;

import java.util.Timer;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.shanglida.R;
import com.shanglida.base.BaseActivity;

/**
 * @author liu
 * @deprecated 这个是个人设置界面的
 * @category 用于一些分享或者更多的内容
 * @date 2014-02-28 AM
 */
public class AboutActivity extends BaseActivity {
	// page2中images的线性布局
	LinearLayout images_layout;
	// 存放布局中的所有image点
	ImageView[] images_point;
	// 存放所有图片
	int[] images;
	// 当前的image
	int mCurImage = 1;
	// 被换的image
	ImageView page_image;
	/** 访问官方网站按钮 **/
	private Button mBtnVisitAppWebsite;
	/** 访问官网微博按钮 **/
	private Button mBtnVisitAppWeiBo;
	/** 客服电话按钮 **/
	private Button mBtnCustomerServiceHotline;
	/** 客服邮箱按钮 **/
	private Button mBtnCustomerServiceMail;
	/** 短信按钮 **/
	private Button mBtnMobilcesms;
	/** 返回按钮 **/
	private Button Sang_set_fan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about_set_activity);
		initView();
		initData();
		// 处理AboutEvent的事件
		AboutEvent();
	}

	private void AboutEvent() {
		// AboutEvent中变量的定义 与 赋值
		images_layout = (LinearLayout) findViewById(R.id.about_layout);
		images_point = new ImageView[images_layout.getChildCount()];
		page_image = (ImageView) findViewById(R.id.about_image);
		images = new int[] { R.drawable.adv1, R.drawable.adv3, R.drawable.adv2,
				R.drawable.adv4 };

		setScrollerImagePoint();// 初始化 AboutEvent中的 滚动点

		final Handler h = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 0x123) {
					if (mCurImage > images.length - 1) {// 如果大于图片的数目 那么重新开始加载
						mCurImage = 0;
					}
					setScrollerImagePoint();
					page_image.setAnimation(AnimationUtils.loadAnimation(
							AboutActivity.this, android.R.anim.fade_out));
					page_image.setImageResource(images[mCurImage]);
					page_image.setAnimation(AnimationUtils.loadAnimation(
							AboutActivity.this, android.R.anim.fade_in));
					mCurImage++;
				}
			}

		};

		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					h.sendEmptyMessage(0x123);
				}
			}
		}, 0, 500);
	}

	/*
	 * 为AboutEvent设置滚动点的效果
	 */
	private void setScrollerImagePoint() {
		for (int i = 0; i < images_layout.getChildCount(); i++) {
			images_point[i] = (ImageView) images_layout.getChildAt(i);
			images_point[i].setEnabled(false);
		}
		images_point[mCurImage].setEnabled(true);// 设置可用的点 图片
	}

	@Override
	protected void initData() {
		mBtnVisitAppWebsite.setOnClickListener(this);
		mBtnVisitAppWeiBo.setOnClickListener(this);
		mBtnCustomerServiceHotline.setOnClickListener(this);
		mBtnCustomerServiceMail.setOnClickListener(this);
		mBtnMobilcesms.setOnClickListener(this);
		Sang_set_fan.setOnClickListener(this);
	}

	@Override
	protected void initView() {
		mBtnVisitAppWebsite = (Button) findViewById(R.id.about_visit_app_website);
		mBtnVisitAppWeiBo = (Button) findViewById(R.id.about_visit_app_weibo);
		mBtnCustomerServiceHotline = (Button) findViewById(R.id.about_customer_service_hotline);
		mBtnCustomerServiceMail = (Button) findViewById(R.id.about_customer_service_mail);
		mBtnMobilcesms = (Button) findViewById(R.id.about_mobilce_sms);
		Sang_set_fan = (Button) findViewById(R.id.sang_set_fan);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 发送短信
		case R.id.about_mobilce_sms:
			SendMsg();
			break;
		// 访问URL网站
		case R.id.about_visit_app_website:
			OpenHttp();
			break;
		// 访问微博
		case R.id.about_visit_app_weibo:
			OpenWeibo();
			break;
		// 联系客服
		case R.id.about_customer_service_hotline:
			CallPhone();
			break;
		// 发送邮件
		case R.id.about_customer_service_mail:
			SendMail();
			break;
		// 返回即关闭页面
		case R.id.sang_set_fan:
			finish();
			break;
		default:
			break;
		}

	}

	private void OpenHttp() {
		OpenWebsite("http://www.51m51m.cn/");
	}

	// 发送邮件
	private void SendMail() {
		Intent data = new Intent(Intent.ACTION_SENDTO);
		data.setData(Uri.parse("mailto:400kf@51m51m.cn"));
		startActivity(data);
	}

	// 联系客服
	private void CallPhone() {
		Intent callIntent = new Intent(Intent.ACTION_DIAL,
				Uri.parse("tel:02037038229"));
		startActivity(callIntent);
	}

	// 打开微博 分类 腾讯微博 新浪微博
	private void OpenWeibo() {
		new AlertDialog.Builder(this)
				.setTitle("访问官方微博")
				.setItems(R.array.weibo_name,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								switch (which) {
								case 0:
									// 新浪微博地址
									OpenWebsite("http://weibo.com/sld51m51m");
									break;

								case 1:
									// 腾讯微博地址
									OpenWebsite("http://t.qq.com/sld51m51m");
									break;
								default:
									break;

								}
							}
						}).create().show();

	}

	// OpenWebsite 打开指定的网页
	private void OpenWebsite(String url) {
		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		Uri content_url = Uri.parse(url);
		intent.setData(content_url);
		startActivity(intent);

	}

	// 发送短信
	private void SendMsg() {
		Intent messageit = new Intent("android.intent.action.SENDTO",
				Uri.parse("smsto:" + ""));
		messageit
				.putExtra(
						"sms_body",
						"我现在用尚礼达免费手机APP，礼品资讯功能强大，操作简单、随时随地发现'尚'机，你也快来试试吧，下载地址 http://www.51m51m.cm/wap/app.aspx 体验好，分享给您朋友。");
		startActivity(messageit);
	}

}
